<?php
/**
 * Class Log created by cephuo
 * Date: 19.09.11
 * Time: 9:45
 */
 
class Log {

	public $path;
	public $level;
	public $filename;
	public $extension;

	function __construct($path = false, $filename = false, $level = false, $extension = false) {
		if ($path) {
			$this->path = $path;
		} else {
			$this->path = conf('LOGS_PATH');
		}
		if ($extension) {
			$this->extension = $extension;
		} else {
			$this->extension = conf('LOGS_EXT');
		}
		if ($filename) {
			$this->filename = $this->path.$filename.$this->extension;
		}
		if ((int)$level > 0) {
			$this->level = (int)$level;
		} else {
			$this->level = 2;
		}
	}

	private function processBacktrace($backtrace) {
		foreach ($backtrace as &$value) {
			$value['args'] = null;
			unset($value['args']);
		}
		return $backtrace;
	}

	/**
	 * This method is an exact copy of Utils::formatDateUser().
	 * Is has to be here because the Log class is used to log
	 * the slow/many queries at the script's shutdown state.
	 *
	 * @param $date
	 * @param bool $includeTime
	 * @return string
	 */
	public function formatDateUser($date, $includeTime = true) {
		if (!is_numeric($date)) {
			if ($includeTime) {
				return date(conf('DATE_TIME_FORMAT_USER'), strtotime($date));
			} else {
				return date(conf('DATE_FORMAT_USER'), strtotime($date));
			}
		} else {
			if ($includeTime) {
				return date(conf('DATE_TIME_FORMAT_USER'), $date);
			} else {
				return date(conf('DATE_FORMAT_USER'), $date);
			}
		}
	}

	public function write($message, $backtrace = NULL) {
		if ($message) {
			$unsetFilename = false;
			$backTraceString = '';
			if (is_array($backtrace) && count($backtrace) > 0) {
				$backtrace = $this->processBacktrace($backtrace);
				$backTraceString = print_r($backtrace, true);
				$caller = array_shift($backtrace);
				if (!$this->filename && ($caller['class'] || $caller['function'] || $caller['line'])) {
					$this->filename = $this->path;
					$caller['class'] = ($caller['class'] ? $caller['class'].'' : '_');
					$caller['function'] = ($caller['function'] ? '_'.$caller['function'] : '');
					$caller['line'] = ($caller['line'] ? '_'.$caller['line'] : '');
					switch ($this->level) {
						case 1 : {
							$this->filename .= $caller['class'];
							break;
						}
						case 2 : {
							$this->filename .= $caller['class'].$caller['function'];
							break;
						}
						case 3 : {
							$this->filename .= $caller['class'].$caller['function'].$caller['line'];
							break;
						}
						default : {
							$this->filename .= $caller['class'].$caller['function'];
							break;
						}
					}
					$this->filename .= $this->extension;
					$unsetFilename = true;
				} else {
					$this->filename = $this->path.conf('LOGS_DEFAULT_NAME').$this->extension;
				}
			} else {
				if (is_string($backtrace)) {
					$backtrace = preg_replace('/[^a-zA-Z0-9\.]/', '_', strtolower($backtrace));
					$this->filename = $this->path.$backtrace.$this->extension;
				} else {
					$this->filename = $this->path.conf('LOGS_DEFAULT_NAME').$this->extension;
				}
			}

			if (!file_exists($this->filename)) {
				file_put_contents($this->filename, '');
			}

			if (is_array($message) || is_object($message)) {
				$message = print_r($message, true);
			}

			$finalMessage = conf('LOGS_SEP');
			$finalMessage .= $this->formatDateUser(time());
			$finalMessage .= "\n";
			$finalMessage .= $message;
			if ($backTraceString != '') {
				$finalMessage .= "\n\nBacktrace:\n";
				$finalMessage .= $backTraceString;
			}
			$finalMessage .= conf('LOGS_SEP');
			$return = file_put_contents($this->filename, $finalMessage, FILE_APPEND | LOCK_EX);
			if ($unsetFilename) {
				$this->filename = false;
			}
			return $return;
		} else {
			return false;
		}
	}

	function __destruct() {
		
	}
}
